home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3270 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: newsroom.hitc.com!usenet
  2. From: psand@eos.hitc.com (G. Patrick Sand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [] overload..(newbie in distress)
  5. Date: 22 Jan 1996 21:11:33 GMT
  6. Organization: Hughes Aircraft (EOSDIS)
  7. Message-ID: <4e0ui5$kjn@newsroom.hitc.com>
  8. References: <DLGppJ.31C@undergrad.math.uwaterloo.ca>
  9. NNTP-Posting-Host: 155.157.118.56
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.3
  12.  
  13. In article <DLGppJ.31C@undergrad.math.uwaterloo.ca>, 
  14. tthiraku@landen.math.uwaterloo.ca says...
  15. >
  16. >Hi..
  17. >
  18. >I'm currently stuck on how to overload [] operator such that
  19. >it does two different tasks..
  20. >
  21. >
  22. >case 1:
  23. >
  24. >// A is an instance of a class that contains a linkist.
  25. >
  26. >   A[5] = val;  // store val into the fifth node of a linklist. 
  27. >   val = A[5] ; // returns the value of the fifth node of a linklist.  
  28. >
  29. >
  30. >I was wondering how can C++ differentiate these two senerios? 
  31. >
  32. >Anything help will greatly appreciated..
  33. >
  34. >Thank You Kindly
  35. >-- 
  36. >Thanou Thirakul
  37. >tthiraku@undergrad.math.uwaterloo.ca
  38. >http://www.undergrad.math.uwaterloo.ca/~tthiraku
  39.  
  40. I think you need the following definitions:
  41.  
  42. //(T is the value class name, ARRAY is the array class name)
  43.  
  44. const T ARRAY::operator[]( int i );  // for the x = a[5] operation
  45. T& ARRAY::operator[]( int i );    // for the a[5] = x operation
  46.  
  47. Haven't tried it but I bet I am on the right track...Of course, I think 
  48. just defining the second one will work...
  49.  
  50. oh- be sure to have a well-defined assignment operator (operator=()) for 
  51. the T class if it isn't one of the pre-defined ones (int, float, double, 
  52. etc.)...
  53.  
  54. Hope this helps...
  55. -- 
  56. G. Patrick Sand
  57. psand@eos.hitc.com
  58. PatSand@aol.com
  59. (301) 925-0791
  60. "Travel Light But Right..."
  61. Microsoft Network is prohibited from redistributing 
  62. this work in any form, in whole or in part.   License 
  63. to distribute this individual post is available to Microsoft
  64. for $999. Posting without permission constitutes an 
  65. agreement to these terms...gps
  66.  
  67.